package org.example.springboot.task;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.example.springboot.component.SseEmitterServer;
import org.example.springboot.entity.*;
import org.example.springboot.service.*;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class accMoistureHandler {

    @Resource
    private IrrigationUnitService irrigationUnitService;

    @Resource
    private FmcMetricsService fmcMetricsService;

    @Resource
    private MoistureControlService moistureControlService;

    @Resource
    private FmcControlService fmcControlService;

    @Resource
    private FmcDataService fmcDataService;

    @Resource
    private SseEmitterServer sseEmitterServer;

    @Scheduled(fixedRate = 500000)
    public void moistureCheck(){
        //查询所有灌溉方式是水分灌溉的单元
        List<IrrigationUnit> irrigationUnitList = irrigationUnitService.list(new QueryWrapper<IrrigationUnit>().eq("type", "03"));
        for (IrrigationUnit irrigationUnit : irrigationUnitList) {
            MoistureControl moistureControl = moistureControlService.getOne(new QueryWrapper<MoistureControl>().eq("irrigation_unit_uuid", irrigationUnit.getUuid()));
            log.info("moistureControl数据：{}",moistureControl);
            String controlDevice = irrigationUnit.getControlDevice();
            FmcMetrics fmcMetrics = fmcMetricsService.getOne(new QueryWrapper<FmcMetrics>().eq("did", controlDevice));
            if(fmcMetrics == null){
                fmcMetrics = new FmcMetrics();
                fmcMetrics.setDid(controlDevice);
                fmcMetrics.setEt(0f);
                fmcMetrics.setIrrigationAmount(0f);
                fmcMetrics.setInitialValue(0f);
                fmcMetrics.setFlowRem(0f);
                fmcMetrics.setSoilMoisture(0f);
                fmcMetricsService.save(fmcMetrics);
            }

            FmcData fmcData = fmcDataService.getOne(new QueryWrapper<FmcData>()
                        .eq("did", moistureControl.getEquipmentId())
                        .orderByDesc("ctime").last("limit 1"));
                float SoilMoisture = fmcMetrics.getSoilMoisture() == null ? 0.0f : fmcMetrics.getSoilMoisture();
                String equipmentValue = moistureControl.getEquipmentValue();
                log.info("fmc_data数据：{}",fmcData);
                switch (equipmentValue){
                    case "c13":
                        SoilMoisture = fmcData.getC13() == null ? 0.0f : fmcData.getC13() + SoilMoisture;
                        break;
                    case "c15":
                        SoilMoisture = fmcData.getC15() == null ? 0.0f : fmcData.getC15() + SoilMoisture;
                        break;
                    case "c17":
                        SoilMoisture = fmcData.getC17() == null ? 0.0f : fmcData.getC17() + SoilMoisture;
                        break;
                }
                fmcMetrics.setSoilMoisture(SoilMoisture);
                fmcMetricsService.updateById(fmcMetrics);
                sseEmitterServer.broadcast("Refresh");
        }
    }

}
